package com.wonder.app.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.SfcProplan;
import com.wonder.app.request.SfcProplanRequest;
import com.wonder.app.service.SfcOptrptPackageService;
import com.wonder.app.service.SfcProplanService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(tags = "工序汇报单")
@RestController
@RequestMapping("/app/SfcProplan")
public class SfcProplanController {
    @Autowired
    private SfcProplanService sfcProplanService;

    @Autowired
    private SysAdminService adminService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SfcOptrptPackageService sfcOptrptPackageService;

    @ApiOperation(value = "生产订单列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody SfcProplanRequest sfcProplanRequest) {
        Page<SfcProplan> page = new Page<SfcProplan>(sfcProplanRequest.getPage(), sfcProplanRequest.getLimit());
        IPage<SfcProplan> list = sfcProplanService.selectList(sfcProplanRequest, page);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data", CommonPage.restPage(list));
        return AjaxMap.success(retMap);
    }

    /**
     * 工序计划单审核
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/audit")
    @ApiOperation(value = "审核", notes = "作者：Wonder")
    public AjaxResult<Boolean> audit(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = sfcProplanService.audit(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    /**
     * 获取工序计划单数据
     * @param map
     * @return
     * @throws Exception
     */
    public Map<String,Object> getProPlan(Map<String,Object> map)throws Exception{
        String billNo = map.get("billNo").toString();
        return sfcProplanService.getRroPlan(billNo);
    }


    @ApiOperation(value = "工序计划单列表(手机端)")
    @RequestMapping(value = "/get_plan_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getPlanlist(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        if(null == map){
            return AjaxResult.failed("system.parameterError");
        }
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Map<String,Object> data = sfcProplanService.getPlanlist(user,map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }


    @ApiOperation(value = "工序计划单列表(手机端模具归还用只查询已经领用模具的)")
    @RequestMapping(value = "/get_plan_list_mold_status", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getPlanlistMoldStatus(@RequestBody Map<String,Object> map) throws Exception{
        if(null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getPlanlistMoldStatus(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查看对象明细(手机端)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询工序计划明细(手机端领料详情)")
    @ResponseBody
    @PostMapping(value = "/get_plan_detail")

    public AjaxResult<Map<String,Object>> getPlanDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getPlanDetail(map,user_id);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查看对象明细(手机端)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询工序计划明细(手机端领用模具)")
    @ResponseBody
    @PostMapping(value = "/get_plan_detail_mold")

    public AjaxResult<Map<String,Object>> getPlanDetailMold(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getPlanDetailMold(map,user_id);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查看对象明细(手机端)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询工序计划明细(手机端归还模具)")
    @ResponseBody
    @PostMapping(value = "/get_plan_detail_mold_return")

    public AjaxResult<Map<String,Object>> getPlanDetailMoldReturn(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getPlanDetailMoldReturn(map,user_id);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查看对象明细(手机端)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询工序计划明细(手机端补料详情)")
    @ResponseBody
    @PostMapping(value = "/get_plan_detail_feed")

    public AjaxResult<Map<String,Object>> getPlanDetailFeed(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getPlanDetailFeed(map,user_id);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }


    @ApiOperation(value = "工序计划单明细(手机端)")
    @RequestMapping(value = "/get_plan_is_detail", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getPlanisDetail(@RequestBody Map<String,Object> map) throws Exception{
        if(null == map){
            return AjaxResult.failed("system.parameterError");
        }

        Map<String,Object> data = sfcProplanService.getPlanisDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询sfcoproplan明细")
    @ResponseBody
    @PostMapping(value = "/sfcoproplandetail")

    public AjaxResult<Map<String,Object>> getSfcProPlanDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getSfcProPlanDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * 根据托盘编号查询对应的订单
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据托盘编号查询对应的订单")
    @RequestMapping(value = "/get_operNo_by_tray", method = RequestMethod.POST)
    public AjaxResult<?> getOperNoByTray(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getOperNoByTray(user_id,map);
        if (!VtuzxUtil.isEmpty(data)){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }


    /**
     * 根据托盘编号查询对应的订单(入库质检和入库时用)
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据托盘编号查询对应的订单(入库质检和入库时用)")
    @RequestMapping(value = "/get_operNo_by_tray_inStock", method = RequestMethod.POST)
    public AjaxResult<?> getOperNoByTrayInStock(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcProplanService.getOperNoByTrayInStock(user_id,map);
        if (!VtuzxUtil.isEmpty(data)){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * 包装间导入领料和入库信息
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/import_report")
    @ApiOperation(value = "包装间导入领料和入库信息", notes = "作者：Wonder")
    public AjaxResult<Object> importReport(HttpServletRequest request,@RequestBody List<Map<String, Object>> map ) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        String userName = user.getName();
        Long user_id = user.getUser_id();
        Boolean res =  sfcOptrptPackageService.importReport(userName,user_id,map);
        return AjaxResult.success(res);


    }

}
